from ultralytics import YOLO


def check_image(image_path, model):
    model = model
    image = image_path
    if image is None:
        # print(f"无法读取图片: {image_path}")
        return None, None
    original_image = image.copy()
    results = model(image, conf=0.5, verbose=False)
    # 提取检测到的第一个目标
    for result in results:
        boxes = result.boxes.xyxy
        if len(boxes) == 0:
            # print("未检测到任何目标")
            return None, original_image
            # continue

        # 取置信度最高的目标
        max_conf_idx = result.boxes.conf.argmax()
        box = boxes[max_conf_idx]
        cls = result.boxes.cls[max_conf_idx]
        conf = result.boxes.conf[max_conf_idx]

        # 裁剪目标区域
        x1, y1, x2, y2 = map(int, box)
        cut_img = original_image[y1:y2, x1:x2]
        return cut_img, original_image

